package cn.edu.neusoft.wuguangsheng.market.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.activity.FoodByIdActivity;
import cn.edu.neusoft.wuguangsheng.market.bean.FoodByShopBean;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.AddCartModel;

/**
 * Created by little on 2018/4/11.
 */

public class FoodByShopAdapter extends BaseAdapter<FoodByShopBean> {


    public FoodByShopAdapter(Context context, List<FoodByShopBean> items,int layoutResource){

        super(context, items, layoutResource);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context)
                .inflate(layoutResourse,parent,false);
        NewFoodByIdViewHolder myViewHolder=new NewFoodByIdViewHolder(itemView);
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewFoodByIdViewHolder cardViewHolder=(NewFoodByIdViewHolder) holder;
        final FoodByShopBean foodByShopBean=items.get(position);
        final String fname=foodByShopBean.getFoodname();
        cardViewHolder.new_name.setText(fname);
          double p=foodByShopBean.getPrice();
        final String price=String.valueOf(p);
        cardViewHolder.new_price.setText("¥"+price);

        final String intro=foodByShopBean.getIntro();
        cardViewHolder.new_intro.setText(intro);
        final String pic=foodByShopBean.getPic();
        final String mrid=foodByShopBean.getMrid();
        final String fid=foodByShopBean.getFid();
        //final String foodprice=foodByShopBean.getPrice();

        Picasso.with(context).load(items.get(position).getPic()).resize(60,60).centerCrop().into(cardViewHolder.new_image);

         cardViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent=new Intent(context, FoodByIdActivity.class);
                 intent.putExtra("fid",fid);
                 intent.putExtra("fname",fname);
                 intent.putExtra("price",price);
                 intent.putExtra("intro",intro);
                 intent.putExtra("pic",pic);
                 intent.putExtra("mrid",mrid);
                 context.startActivity(intent);
             }
         });


        cardViewHolder.cart_by_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp;
                sp=context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
                String uid = sp.getString("userid","");
                //String uid=context.getString(uid);
                String num="1";
                double sum=foodByShopBean.getPrice();
                AddCartModel addCartModel=new AddCartModel();
                addCartModel.addCart(uid,fid,num,sum,mrid,addListener);
                //Toast.makeText(context,uid,Toast.LENGTH_SHORT).show();
            }
        });
    }
    TListener<RegisterBean> addListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
    String success=registerBean.getSuccess();
            if(success.equals("1")){
                Toast.makeText(context,"添加购物车成功",Toast.LENGTH_SHORT).show();
            }
            else if(success.equals("2")){
                Toast.makeText(context,"购物车中已有该菜品",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(context,"添加失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"访问失败",Toast.LENGTH_SHORT).show();
        }
    };


}
